﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ToolsLayer;
using BusinessEntity;
using PersistenceLayer;
using System.Collections.Generic;
namespace Webcar.BLL
{
    public class BaseCodeManager
    {
        private PersistenceLayer.EntityObject ent = null;
        public EntityObject Entity
        {
            get { return ent; }
        }
        public string BaseCode
        {
            set
            {
                if (ent is BS_CAR_CLASSEntity)
                    ((BS_CAR_CLASSEntity)ent).CAR_CLASS = value;
                else
                    ((BS_CODEEntity)ent).BASE_CODE = value;
            }
            get
            {
                if (ent is BS_CAR_CLASSEntity)
                    return ((BS_CAR_CLASSEntity)ent).CAR_CLASS;
                else
                    return ((BS_CODEEntity)ent).BASE_KIND;
            }
        }
        public string BaseName
        {
            set
            {
                if (ent is BS_CAR_CLASSEntity)
                    ((BS_CAR_CLASSEntity)ent).CAR_NOTE = value;
                else
                    ((BS_CODEEntity)ent).BASE_NAME = value;
            }
            get
            {
                if (ent is BS_CAR_CLASSEntity)
                    return ((BS_CAR_CLASSEntity)ent).CAR_NOTE;
                else
                    return ((BS_CODEEntity)ent).BASE_NAME;
            }
        }
        public string BaseKind
        {
            set
            {
                if (ent is BS_CAR_CLASSEntity)
                    ((BS_CAR_CLASSEntity)ent).CAR_SORT = value;
                else
                    ((BS_CODEEntity)ent).BASE_KIND = value;
            }
            get
            {
                if (ent is BS_CAR_CLASSEntity)
                    return ((BS_CAR_CLASSEntity)ent).CAR_SORT;
                else
                    return ((BS_CODEEntity)ent).BASE_KIND;
            }
        }
        public BaseCodeManager(string kind,string code)
        {
            //如果kind不是数字，说明是选择的不是车型小类
            if (!PublicFunction.IsNumber(kind))
            {
                ent = new  BS_CODEEntity();
                ((BS_CODEEntity)ent).BASE_CODE = code;
                ent.Retrieve();
            }
            else
            {
                ent = new BS_CAR_CLASSEntity();
                ((BS_CAR_CLASSEntity)ent).CAR_CLASS = code;
                ent.Retrieve();
            }
        }

        public string Save()
        {
            string newcode = "";
            if (!ent.IsPersistent)
            {
                newcode = GetNewCode();
                if (ent is BS_CAR_CLASSEntity)
                    ((BS_CAR_CLASSEntity)ent).CAR_CLASS = newcode;
                else
                    ((BS_CODEEntity)ent).BASE_CODE = newcode;
            }
            ent.Save();

            return newcode;
        }

        public bool CanDeleteCarClass()
        {
            BS_CAR_CLASSEntity c = (BS_CAR_CLASSEntity)ent;
            string sql = "select count(1) from bs_car_record where car_class='" + c.CAR_CLASS + "'";
            return DB.ExecuteScalar(sql) == "0";
        }
        public bool CanDeleteBaseCode()
        {
            List<string> s = new List<string>();
            //s.Add("A,bs_emp,job_kind");
            s.Add("B,rp_car_repair_head,repair_kind");//维修类型
            s.Add("C,bs_car_record,asset_dept");//车辆归属
            s.Add("D,sec_record,sec_kind");//安全事故类型
            s.Add("E,bs_car_class,car_sort");
            s.Add("H,dept,dept_kind");

            BS_CODEEntity c = (BS_CODEEntity)ent;
            foreach (string str in s)
            {
                string[] sa = str.Split(new char[]{','});
                if(sa[0]==c.BASE_KIND)
                {
                    string sql = "select count(1) from " + sa[1] + " where " + sa[2] + "='" + c.BASE_CODE + "'";
                    string ret = DB.ExecuteScalar(sql);
                    if (ret != "0")
                        return false;
                }
            }

            return true;
        }

        public bool CanDelete()
        {
            if (ent is BS_CAR_CLASSEntity)
                return this.CanDeleteCarClass();
            else
                return this.CanDeleteBaseCode();
        }
        public void DeleteCode()
        {
            if (!ent.IsPersistent)
                return;
            if (this.CanDelete())
            {
                if (ent.Delete() <= 0)
                    throw new WBException(WBErrorType.DeleteCascadeError);
            }
            else
            {
                throw new Exception("该代码已经关联，不能被删除。");
            }
        }

        public string GetNewCode()
        {
            string sql = "select max(cast(base_code as int)) from v_base_code";
            string ret = DB.ExecuteScalar(sql);
            int r = Int32.Parse(ret) + 1;
            return r.ToString();
        }
    }
}
